package com.zk.iotgateway.rabbitmq;

import com.rabbitmq.client.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * 自动执行，从数据库获取信息，并且传递参数创建消息中心和队列。
 * @author:mn.m
 * @create 2019-04-25 上午 10:39
 **/
@Component
public class IotRabbitConfig implements ApplicationRunner {

    public static final  Map<String, PublisherConfig> rabbitTemplateMap = new ConcurrentHashMap<>();
    public static final Map<String,String> dataQueueNameMap = new ConcurrentHashMap<>();
    public static final Map<String, Channel> channelMap = new ConcurrentHashMap<>();

    private  String host1 = "";
    private  String host = "";
    private  int port = 0;
    private  String username = "";
    private  String password = "";
    private  String username1 = "";
    private  String password1 = "";
    private  String dataQueueName = "";
    private  String commandQueueName = "";
    private  String commandExchange = "";
    private String bindkey = "";
    private String virtualHost;

    @Autowired
    private IotRabbitCreator iotRabbitCreator;
    /*
    * 需要注入配置类
    * */

    @Override
    public void run(ApplicationArguments args) throws Exception {
        //获取所有的rabbitmq配置信息


        //根据查出的配置信息，创建队列并绑定
       /* host ="192.168.1.93";
        host1 ="120.24.48.131";
        port = 5672;
       username1 ="platform";
        password1 ="zhongkai#2018";
        username ="admin";
        password ="123456";
        dataQueueName = "workcard";
        commandQueueName = "workcard,workcard1";
        commandExchange ="wc_exchange";
        bindkey = "workcard";
        virtualHost="/";*/
        //获得channel
      /*  Channel channel = iotRabbitCreator.creatRabbitChannel(host, port, username, password,virtualHost);
       channel.addConfirmListener(new ConfirmCallback() {
           @Override
           public void handle(long deliveryTag, boolean multiple) throws IOException {

           }
       },null);
        *//*
        * 消息从交换器发送到对应队列失败时触发（比如根据发送消息时指定的routingKey找不到队列时会触发）
        * *//*
        channel.addReturnListener(new ReturnCallback() {
            @Override
            public void handle(Return returnMessage) {
                System.out.println(returnMessage.getReplyCode()+returnMessage.getReplyText());
            }
        });
        //声明队列
        *//**
         * 申明一个队列，如果这个队列不存在，将会被创建
         * @param queue 队列名称
         * @param durable 持久性：true队列会再重启过后存在，但是其中的消息不会存在。
         * @param exclusive 是否只能由创建者使用，其他连接不能使用。
         * @param autoDelete 是否自动删除（没有连接自动删除）
         * @param arguments 队列的其他属性(构造参数)
         * @return Queue.DeclareOk：宣告队列的声明确认方法已成功声明。
         * @throws java.io.IOException if an error is encountered
         *//*

       *//* channel.queueDeclare(dataQueueName, true, false, false, null);*//*
        channel.queueDeclare(commandQueueName, true, false, false, null);

        *//**
         * 声明一个 exchange.
         * @param exchange 名称
         * @param type  exchange type：direct、fanout、topic、headers
         * @param durable 持久化
         * @param autoDelete 是否自动删除（没有连接自动删除）
         * @param arguments 队列的其他属性(构造参数)
         * @return 成功地声明了一个声明确认方法来指示交换。
         * @throws java.io.IOException if an error is encountered
         *//*
        channel.exchangeDeclare(commandExchange, BuiltinExchangeType.DIRECT, true, false, null);

        //绑定路由和队列(String queue, boolean durable, boolean exclusive, boolean autoDelete,Map<String, Object> arguments)
        channel.queueBind(commandQueueName, commandExchange, bindkey);

        //将channel和queueName作为value放在对应的map中，key为port
       channelMap.put("9811",channel);
       dataQueueNameMap.put("9811",commandQueueName);*/

       /* IotRabbitMqDO iotRabbitMqDO =new IotRabbitMqDO();
        iotRabbitMqDO.setAddresses(host);
        iotRabbitMqDO.setUsername(username);
        iotRabbitMqDO.setPassword(password);
        iotRabbitMqDO.setPort(port);
        iotRabbitMqDO.setPublisherConfirms(true);
        iotRabbitMqDO.setQueueName(commandQueueName);
        iotRabbitMqDO.setExchange(commandExchange);
        iotRabbitMqDO.setVirtualHost(virtualHost);
        iotRabbitMqDO.setRoutingKey(bindkey);
        iotRabbitMqDO.setExchangeType(BuiltinExchangeType.DIRECT.getType());
        PublisherConfig publisherConfig =new PublisherConfig();
        publisherConfig.initConfig(iotRabbitMqDO);
        rabbitTemplateMap.put("9811",publisherConfig);*/

       /* IotRabbitMqDO iotRabbitMqDO1 =new IotRabbitMqDO();
        iotRabbitMqDO1.setAddresses(host1);
        iotRabbitMqDO1.setUsername(username1);
        iotRabbitMqDO1.setPassword(password1);
        iotRabbitMqDO1.setPort(port);
        iotRabbitMqDO1.setPublisherConfirms(true);
        iotRabbitMqDO1.setQueueName(dataQueueName);
        iotRabbitMqDO1.setExchange(commandExchange);
        iotRabbitMqDO1.setVirtualHost(virtualHost);
        iotRabbitMqDO1.setRoutingKey(bindkey);
        iotRabbitMqDO1.setExchangeType(BuiltinExchangeType.DIRECT.getType());
        PublisherConfig publisherConfig1 =new PublisherConfig();
        publisherConfig1.initConfig(iotRabbitMqDO1);
        rabbitTemplateMap.put("9812",publisherConfig1);*/
    }
}
